﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Utility
{
    public class DBUtils
    {
        public static readonly DateTime BaseDateTime = new DateTime(1970, 1, 1);

        internal static void MakeParm(System.Data.Common.DbParameter parm, char prefix)
        {
            if (parm != null)
            {
                string parmName = parm.ParameterName;
                if (!string.IsNullOrEmpty(parmName) && parmName.IndexOf(prefix) != 0)
                {
                    parm.ParameterName = prefix + parmName;
                }
            }
        }

        /// <summary>
        /// make 'like' parameterName for mysql
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static string MakeMySqlParm(string parameterName)
        {
            return string.Format("CONCAT('%',?{0},'%')", parameterName);
        }

        /// <summary>
        /// make 'like' parameterName for SQLServer
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static string MakeSQLServerParm(string parameterName)
        {
            return string.Format("'%' + @{0} + '%'",parameterName);
        }

        /// <summary>
        /// make 'like' parameterName for oracle
        /// </summary>
        /// <param name="parmeterName"></param>
        /// <returns></returns>
        public static string MakeOracleParm(string parmeterName)
        {
            return string.Format("CONCAT('%',CONCAT(:{0},'%'))", parmeterName);
        }

        public static string CombColNM(string tableName, string columnName)
        {
            return string.Format("{0}_{1}", tableName, columnName);
        }

        public static bool? GetNullBool(object obj)
        {
            if (IsNull(obj))
            {
                return null;
            }

            try
            {
                if (obj is string)
                {
                    return ("1".Equals(obj) ||
                            "true".Equals((string)obj, StringComparison.OrdinalIgnoreCase)
                            ) ? true : false;

                }

                return Convert.ToBoolean(obj);
            }
            catch
            {
                return null;
            }
        }

        public static bool GetBool(object obj)
        {
            return GetBool(obj, false);
        }

        public static bool GetBool(object obj, bool defaultVal)
        {
            if (IsNull(obj))
            {
                return defaultVal;
            }

            try
            {
                if(obj is string)
                {
                    return ("1".Equals(obj) ||
                            "true".Equals((string)obj,StringComparison.OrdinalIgnoreCase)
                            ) ? true : false;
                        
                }

                return Convert.ToBoolean(obj);
            }
            catch
            {
                return defaultVal;
            }
        }

        public static DateTime? GetNullDateTime(object obj)
        {
            if (IsNull(obj))
            {
                return null;
            }

            try
            {
                if (obj is TimeSpan)
                {
                    return BaseDateTime.Add((TimeSpan)obj);
                }

                return Convert.ToDateTime(obj);
            }
            catch
            {
                return null;
            }
        }

        public static DateTime GetDateTime(object obj)
        {
            return GetDateTime(obj, new DateTime());
        }

        public static DateTime GetDateTime(object obj, DateTime defaultVal)
        {
            if (IsNull(obj))
            {
                return defaultVal;
            }

            try
            {
                if (obj is TimeSpan)
                {
                    return BaseDateTime.Add((TimeSpan)obj);
                }

                return Convert.ToDateTime(obj);
            }
            catch
            {
                return defaultVal;
            }
        }

        public static int? GetNullInt(object obj)
        {
            if (IsNull(obj))
            {
                return null;
            }

            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return null;
            }
        }

        public static int GetInt(object obj)
        {
            return GetInt(obj, 0);
        }

        public static int GetInt(object obj, int defaultVal)
        {
            if (IsNull(obj))
            {
                return defaultVal;
            }

            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return defaultVal;
            }
        }

        public static double? GetNullDouble(object obj)
        {
            if (IsNull(obj))
            {
                return null;
            }

            try
            {
                return Convert.ToDouble(obj);
            }
            catch
            {
                return null;
            }
        }

        public static double GetDouble(object obj)
        {
            return GetDouble(obj, 0.0);
        }

        public static double GetDouble(object obj, double defaultVal)
        {
            if (IsNull(obj))
            {
                return defaultVal;
            }

            try
            {
                return Convert.ToDouble(obj);
            }
            catch
            {
                return defaultVal;
            }
        }

        public static long? GetNullLong(object obj)
        {
            if (IsNull(obj))
            {
                return null;
            }

            try
            {
                return Convert.ToInt64(obj);
            }
            catch
            {
                return null;
            }
        }

        public static long GetLong(object obj)
        {
            return GetLong(obj, 0L);
        }

        public static long GetLong(object obj, long defaultVal)
        {
            if (IsNull(obj))
            {
                return defaultVal;
            }

            try
            {
                return Convert.ToInt64(obj);
            }
            catch
            {
                return defaultVal;
            }
        }

        public static float? GetNuLLFloat(object obj)
        {
            if (IsNull(obj))
            {
                return null;
            }

            try
            {
                return Convert.ToSingle(obj);
            }
            catch
            {
                return null;
            }
        }

        public static float GetFloat(object obj)
        {
            return GetFloat(obj, 0.0f);
        }

        public static float GetFloat(object obj, float defaultVal)
        {
            if (IsNull(obj))
            {
                return defaultVal;
            }
            try
            {
                return Convert.ToSingle(obj);
            }
            catch
            {
                return defaultVal;
            }
        }

        public static string GetStr(object obj)
        {
            return obj as string;
        }

        public static string GetStr(object obj, string defaultVal)
        {
            return (obj as string) ?? defaultVal;
        }

        public static bool IsNull(object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return true;
            }

            return false;
        }

        public static string MakeInStr(string[] parms)
        {
            StringBuilder strParm = new StringBuilder();
            if (parms != null)
            {
                foreach (string item in parms)
                {
                    strParm.AppendFormat("'{0}',", item);
                }
            }
            return strParm.ToString().TrimEnd(',');
        }
    }
}
